home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1995 Fall / PD-ROM F95.toast / Programming / Programming Languages / UCB Logo 3.0 ƒ / sources / standard source / print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  6.5 KB  |  307 lines  |  [TEXT/ttxt]

  1. /*
  2.  *      print.c         logo printing module                    dvb
  3.  *
  4.  *    Copyright (C) 1993 by the Regents of the University of California
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *  
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *  
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "logo.h"
  22. #include "globals.h"
  23. #include <stdarg.h>
  24.  
  25. int print_stringlen;
  26. char *print_stringptr;
  27. extern void real_print_help(), real_print_node();
  28. int x_margin=0, y_margin=0;
  29.  
  30. BOOLEAN print_backslashes = FALSE;
  31.  
  32. #ifdef mac
  33. BOOLEAN boldmode = 0;
  34. #endif
  35. #ifdef ibm
  36. extern BOOLEAN in_graphics_mode;
  37. #endif
  38.  
  39. void update_coords(char ch)
  40. {
  41.     int i;
  42.  
  43. #ifdef ibm
  44. #ifndef __ZTC__
  45.     check_scroll();
  46. #endif
  47. #endif
  48.     if (ch == '\n') {
  49.     x_coord = 0;
  50.     y_coord++;
  51.     for (i = 0; i < x_margin; i++) print_char(stdout,' ');
  52.     } else if (ch == '\b') {
  53.     if (x_coord > 0) --x_coord;
  54.     } else if (ch == '\t') {
  55.     x_coord &= ~07;
  56.     x_coord += 8;
  57.     } else if (ch != '\007')
  58.     x_coord++;
  59.     if (x_coord > x_max) {
  60.     y_coord += x_coord/x_max;
  61.     x_coord %= x_max;
  62.     }
  63.     if (y_coord > y_max) y_coord = y_max;
  64. }
  65.  
  66. void print_char(FILE *strm, char ch)
  67. {
  68.     if (strm) {
  69.     if (interactive && strm==stdout) {
  70. #ifdef mac
  71.         if (boldmode) {
  72.         if (ch == '\2')
  73.             boldmode = 0;
  74.         else
  75.             ch = setparity(ch);
  76.         } else if (ch == '\1')
  77.         boldmode = 1;
  78. #endif
  79. #ifdef ibm
  80.         if (ch == '\1')
  81.         ibm_bold_mode();
  82.         if (ch == '\2')
  83.         ibm_plain_mode();
  84. #ifdef __ZTC__
  85.         ztc_put_char(ch);
  86. #else
  87.         if (in_graphics_mode && ibm_screen_top == 0)
  88.         lsplitscreen();
  89.         if (ch == '\n' || in_graphics_mode)
  90.         putc(ch, strm);
  91.         else if (ch != '\1' && ch != '\2')
  92.         putch(ch); /* takes advantage of bold attribute */
  93. #endif
  94. #else
  95.         putc(ch, strm);
  96. #endif
  97.     } else        /* printing to stream but not screen */
  98.         putc(ch, strm);
  99.     if (strm == stdout) {
  100.         if (dribblestream != NULL)
  101.         putc(ch, dribblestream);
  102.         update_coords(ch);
  103.     }
  104.     } else {        /* printing to string */
  105.     if (--print_stringlen > 0)
  106.         *print_stringptr++ = ch;
  107.     }
  108. }
  109.  
  110. void print_space(FILE *strm)
  111. {
  112.     print_char(strm,' ');
  113. }
  114.  
  115. /*VARARGS2*/
  116. void ndprintf(FILE *strm, char *fmt, ...)
  117. {
  118.     va_list ap;
  119.     NODE *nd;
  120.     char *cp;
  121.     char ch;
  122.  
  123.     va_start(ap,fmt);
  124.     while ((ch = *fmt++) != '\0') {
  125.     if (ch == '%') {
  126.         ch = *fmt++;
  127.         if (ch == 's')    /* show */
  128.         print_node(strm,va_arg(ap,NODE *));
  129.         else if (ch == 'p') { /* print */
  130.         nd = va_arg(ap,NODE *);
  131.         if (is_list(nd))
  132.             print_help(strm,nd);
  133.         else
  134.             print_node(strm,nd);
  135.         } else if (ch == 't') { /* text */
  136.         cp = va_arg(ap,char *);
  137.         while (ch = *cp++) print_char(strm,ch);
  138.         } else {
  139.         print_char(strm,'%');
  140.         print_char(strm,ch);
  141.         }
  142.     } else print_char(strm,ch);
  143.     }
  144.     va_end(ap);
  145. }
  146.  
  147. void real_print_help(FILE *strm, NODE *ndlist, int depth, int width)
  148. {
  149.     NODE *arg = NIL;
  150.     int wid = width;
  151.  
  152.     while (ndlist != NIL) {
  153.     arg = car(ndlist);
  154.     ndlist = cdr(ndlist);
  155.     if (check_throwing) break;
  156.     real_print_node(strm, arg, depth, width);
  157.     if (ndlist != NIL) {
  158.         print_space(strm);
  159.         if (--wid == 0) {
  160.         ndprintf(strm, "...");
  161.         break;
  162.         }
  163.     }
  164.     }
  165. }
  166.  
  167. void real_print_node(FILE *strm, NODE *nd, int depth, int width)
  168. {
  169.     int i;
  170.     char *cp;
  171.     NODETYPES ndty;
  172.  
  173.     if (depth == 0) {
  174.     ndprintf(strm, "...");
  175.     return;
  176.     }
  177.     if (nd == NIL) {
  178.     print_char(strm,'[');
  179.     print_char(strm,']');
  180.     } else if (nd == UNBOUND) {
  181.     ndprintf(strm, "UNBOUND");
  182.     } else if ((ndty = nodetype(nd)) & NT_PRIM) {
  183.     ndprintf(strm, "PRIM");
  184.     } else if (ndty & NT_LIST) {
  185.     print_char(strm,'[');
  186.     real_print_help(strm, nd, depth-1, width);
  187.     print_char(strm,']');
  188.     } else if (ndty == ARRAY) {
  189.     int i = 0, dim = getarrdim(nd), wid;
  190.     NODE **pp = getarrptr(nd);
  191.  
  192.     if (width < 0) wid = dim;
  193.     else wid = (dim > width ? width : dim);
  194.     print_char(strm,'{');
  195.     while (i < wid) {
  196.         real_print_node(strm,*pp++,depth-1,width);
  197.         if (++i < dim) print_space(strm);
  198.     }
  199.     if (wid < dim) ndprintf(strm, "...");
  200.     print_char(strm,'}');
  201.     if (print_backslashes && (getarrorg(nd) != 1)) {
  202.         char org[] = "@    ";
  203.  
  204.         sprintf(&org[1],"%d",getarrorg(nd));
  205.         ndprintf(strm,org);
  206.     }
  207.     } else if (ndty == QUOTE) {
  208.     print_char(strm, '\"');
  209.     print_node(strm, car(nd));
  210.     } else {
  211.     int wid;
  212.  
  213.     nd = cnv_node_to_strnode(nd);
  214.     cp = getstrptr(nd);
  215.     if (width < 0) wid = getstrlen(nd);
  216.     else {
  217.         wid = (width < 10 ? 10 : width);
  218.         wid = (wid < getstrlen(nd) ? wid : getstrlen(nd));
  219.     }
  220.  
  221.     if (print_backslashes == FALSE)
  222.         for (i = 0; i < wid; i++) {
  223.         print_char(strm,clearparity(*cp++));
  224.         }
  225.     else
  226.         for (i = 0; i < wid; i++) {
  227.         if (getparity(*cp)) {
  228.             print_char(strm,'\\');
  229.         }
  230.         print_char(strm,clearparity(*cp++));
  231.         }
  232.     if (wid < getstrlen(nd)) ndprintf(strm, "...");
  233.     gcref(nd);
  234.     }
  235. }
  236.  
  237. int find_limit(NODE *nd)
  238. {
  239.     int val = -1;
  240.  
  241.     if (nd == NIL) return(-1);
  242.     nd = cnv_node_to_numnode(valnode__caseobj(nd));
  243.     if (nodetype(nd) == INT) val = getint(nd);
  244.     gcref(nd);
  245.     return(val);
  246. }
  247.  
  248. void print_help(FILE *strm, NODE *nd)
  249. {
  250.     real_print_help(strm, nd, find_limit(Printdepthlimit),
  251.             find_limit(Printwidthlimit));
  252. }
  253.  
  254. void print_node(FILE *strm, NODE *nd)
  255. {
  256.     real_print_node(strm, nd, find_limit(Printdepthlimit),
  257.             find_limit(Printwidthlimit));
  258. }
  259.  
  260. void print_nobrak(FILE *strm, NODE *nd)
  261. {
  262.     if (is_list(nd)) print_help(strm, nd);
  263.     else print_node(strm, nd);
  264. }
  265.  
  266. void new_line(FILE *strm)
  267. {
  268.     print_char(strm,'\n');
  269. }
  270.  
  271. NODE *lshow(NODE *args)
  272. {
  273.     print_help(writestream, args);
  274.     new_line(writestream);
  275.     return(UNBOUND);
  276. }
  277.  
  278. void type_help(NODE *args, int sp)
  279. {
  280.     NODE *arg = NIL;
  281.  
  282.     while (args != NIL) {
  283.     arg = car(args);
  284.     args = cdr(args);
  285.     if (is_list(arg))
  286.         print_help(writestream, arg);
  287.     else
  288.         print_node(writestream, arg);
  289.     if (sp && (args != NIL)) {
  290.         print_space(writestream);
  291.     }
  292.     }
  293. }
  294.  
  295. NODE *ltype(NODE *args)
  296. {
  297.     type_help(args,0);
  298.     return(UNBOUND);
  299. }
  300.  
  301. NODE *lprint(NODE *args)
  302. {
  303.     type_help(args,1);
  304.     new_line(writestream);
  305.     return(UNBOUND);
  306. }
  307.